package cn.com.guage.transtation;
class TccAccount {
    private String name;
    private int balance;       // 实际余额
    private int frozen;        // 冻结金额（Try阶段预留）

    public TccAccount(String name, int balance) {
        this.name = name;
        this.balance = balance;
        this.frozen = 0;
    }

    // Try 阶段：冻结金额
    public boolean tryTransfer(int amount) {
        if (balance >= amount) {
            balance -= amount;
            frozen += amount;
            return true;
        }
        return false;
    }

    // Confirm 阶段：确认转账（无实际操作，因为金额已冻结）
    public void confirmTransfer(int amount) {
        frozen -= amount; // 释放冻结金额标记（实际金额已在Try阶段扣除）
    }

    // Cancel 阶段：回滚冻结金额
    public void cancelTransfer(int amount) {
        balance += amount;
        frozen -= amount;
    }

    public String getName() { return name; }
    public int getBalance() { return balance; }
    public int getFrozen() { return frozen; }
}

class TccCoordinator {
    private TccAccount from;
    private TccAccount to;
    private int amount;
    private boolean trySuccess = false;

    public boolean isTrySuccess() {
        return trySuccess;
    }

    public TccCoordinator(TccAccount from, TccAccount to, int amount) {
        this.from = from;
        this.to = to;
        this.amount = amount;
    }

    // Try 阶段（资源预留）
    public boolean tryPhase() {
        boolean fromSuccess = from.tryTransfer(amount);
        boolean toSuccess = to.tryTransfer(-amount); // 接收方只需预留额度（负数表示可接收）
        trySuccess = fromSuccess && toSuccess;
        return trySuccess;
    }

    // Confirm 阶段（提交事务）
    public void confirmPhase() {
        if (trySuccess) {
            from.confirmTransfer(amount);
            to.confirmTransfer(-amount);
        }
    }

    // Cancel 阶段（回滚事务）
    public void cancelPhase() {
        if (trySuccess) {
            from.cancelTransfer(amount);
            to.cancelTransfer(-amount);
        }
    }
}

public class TccBankTransfer {
    public static void main(String[] args) {
        TccAccount zhangSan = new TccAccount("张三", 1000);
        TccAccount liSi = new TccAccount("李四", 1000);
        int transferAmount = 500;
        TccCoordinator coordinator = new TccCoordinator(zhangSan, liSi, transferAmount);

        System.out.println("======== 初始状态 ========");
        printAccounts(zhangSan, liSi);

        // Try 阶段
        System.out.println("\n======== Try 阶段（资源预留） ========");
        if (coordinator.tryPhase()) {
            System.out.println("Try 成功：金额已冻结");
        } else {
            System.out.println("Try 失败：回滚事务");
            coordinator.cancelPhase();
        }
        printAccounts(zhangSan, liSi);

        // Confirm 阶段（假设 Try 成功）
        if (coordinator.isTrySuccess()) {
            System.out.println("\n======== Confirm 阶段（提交事务） ========");
            coordinator.confirmPhase();
            printAccounts(zhangSan, liSi);
        }
    }

    private static void printAccounts(TccAccount a1, TccAccount a2) {
        System.out.println("账户名称 | 实际余额 | 冻结金额");
        System.out.printf("%s | %d | %d\n", a1.getName(), a1.getBalance(), a1.getFrozen());
        System.out.printf("%s | %d | %d\n", a2.getName(), a2.getBalance(), a2.getFrozen());
    }
}